import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class test111 {

    public static void main(String[] args) throws IOException {
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        String s = sc.readLine();
        String substring = s.substring(2, s.length() - 2);
        String[] split = substring.split("],");
        int[][] grid1 = new int[split.length][(split[0].length()+1)/2];
        int temp = 0;
        for (int i = 0; i < split[0].length(); i = i+2) {
            grid1[0][temp++] = (int)(split[0].charAt(i))-48;
        }
        for (int i = 1; i <split.length ; i++) {
            temp = 0;
            for (int j = 1; j < split[i].length(); j=j+2) {
                grid1[i][temp++] = (int)(split[i].charAt(j))-48;

            }
        }
        for (int i = 0; i < grid1.length; i++) {
            for (int j = 0; j < grid1[0].length; j++) {
                if(grid1[i][j] == 2) {
                    grid1[i][j] = 99999;
                } else if(grid1[i][j] == 0) {
                    grid1[i][j] = 2;
                }
            }
        }
        int i = minPathSum(grid1);
        System.out.println(i>99999 ?-1:i);
    }
    public static int minPathSum(int[][] grid){
        if (grid==null || grid.length == 0||grid[0].length == 0){
            return 0;
        }
        int rows = grid.length;
        int cols = grid[0].length;
        int[][] data = new int[rows][cols];
        for (int i = 1; i < rows; ++i)
        {
            data[i][0] = data[i - 1][0] + grid[i][0];
        }
        for (int i = 1; i < cols; ++i)
        {
            data[0][i] = data[0][i - 1] + grid[0][i];
        }
        for (int i = 1; i < rows; ++i)
        {
            for (int j = 1; j < cols; ++j)
            {
                data[i][j] = Math.min(data[i][j - 1], data[i - 1][j]) + grid[i][j];
            }
        }
        return data[rows - 1][cols - 1];
    }
}

